package com.kingstar.runable;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kingstar.entity.CbondaccruedinterestEntity;
import com.kingstar.mapper.BasicMapper;
import com.kingstar.service.ICbondaccruedinterestService;
import org.apache.commons.lang3.time.StopWatch;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class SqlTest implements ApplicationRunner {

    private final ICbondaccruedinterestService cbondaccruedinterestService;

    @Resource
    private BasicMapper basicMapper;

    public SqlTest(ICbondaccruedinterestService cbondaccruedinterestService) {
        this.cbondaccruedinterestService = cbondaccruedinterestService;
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {

        StopWatch stopWatch=new StopWatch();
        stopWatch.start();
        extracted2();
        stopWatch.stop();
        System.out.println("耗时: "+stopWatch.getTime());
    }

    private void extracted2() {
        basicMapper.doCarryForward("CBONDACCRUEDINTEREST","HIS_CBONDACCRUEDINTEREST");
//        basicMapper.doCarryForward();
    }

    private void extracted1() {
        IPage<CbondaccruedinterestEntity> page=new Page<>();
        int limit_page=1000;
        page.setSize(limit_page);
        final int count = cbondaccruedinterestService.count();
        int total=count % limit_page ==0 ? count/limit_page:count/limit_page+1;
        for ( int i = 0; i < total; i++) {
            // 分页查询插入
            long time0 = System.currentTimeMillis();
            page.setCurrent(i+1);
            IPage<CbondaccruedinterestEntity> page1 = cbondaccruedinterestService.page(page);
            long time1 = System.currentTimeMillis();
            System.out.println(String.format("query time1:%s,耗时:%s", time1, time1 - time0));
//            if(page1!=null){
//                List<CbondaccruedinterestEntity> list = page1.getRecords();
//                // 2. 插入数据到历史表中
//                List<HisCbondaccruedinterestEntity> hisEntityList=copyPrioperties(list);
//                long time2 = System.currentTimeMillis();
//                log.info("query time2：{},耗时{}", time2, time2 - time1);
//                boolean succ = hisService.saveBatch(hisEntityList);
//                long time3 = System.currentTimeMillis();
//                log.info("query time3：{},耗时{}", time3, time3 - time2);
//                log.info("结转 {} ，第{}次结转数据{}条",currEntity.getClass(),(i+1),list.size());
//            }
        }
    }

//    private List<HisCbondaccruedinterestEntity> copyPrioperties(List<CbondaccruedinterestEntity> list) {
//        return (List<HisCbondaccruedinterestEntity>)list;
//    }

    private void extracted() {
        final int count = cbondaccruedinterestService.count();
        System.out.println("==================: "+count);
    }
}
